export default {
  data() {
    return {
      gfdimg: [], //图片合集
      Seriesid: 0, //系列id
      type: 0, //0官方素材/1平台素材
      currentimg: '', //当前图片
      panduan: '', //判断是否为多选110
      currentimgList: [], //当前图片lilst
      imgsum: 0, //上一页图片数量
      showPage: false
    }
  },
  created() {
    this.GetMaterialLibraryDataList() // 获取图片素材库
  },
  onLoad(carvule) {
    this.Seriesid = carvule.Seriesid
    this.type = carvule.type
    this.panduan = carvule.panduan
    this.imgsum = Number(carvule.imgsum)
  },
  methods: {
    // 获取图片素材库
    async GetMaterialLibraryDataList() {
      try {
        let data = {
          'Type': this.type, //0官方素材/1平台素材
          'CarTypeId': this.Seriesid, //车型系列id
        }
        const res = await this.$api.carSource.GetMaterialLibraryDataList(data)
        //增加字段
        this.selected(res.data.list, chked => {
          this.gfdimg = chked
        })
        this.showPage = true
      } catch (err) {
        console.log(err)
      }
    },
    //增加字段
    selected(list, callback) {
      let arr = JSON.parse(JSON.stringify(list))
      arr.forEach(item => {
        if (item.img !== null) {
          item.chked = false
        }
      })
      callback(arr)
    },
    //选择图片
    GFclickimg(curr, index) {
      for (var i = 0; i < this.gfdimg.length; i++) {

        // 为110多选
        if (this.panduan === '110') {
          // 选择的总数量，不超过5张
          // if (this.imgsum < 5) {
          if (i === index) {
            if (this.gfdimg[index].chked === false) {
              if (this.imgsum < 5) {
                this.gfdimg[index].chked = true
                this.currentimgList.push(this.gfdimg[index])
                console.log(111, this.currentimgList)
                this.imgsum++
              } else {
                this.$refs.uToast.show({
                  title: '最多选择5张图片',
                  type: 'warning',
                  url: '/pages/user/index'
                })
              }
            } else {
              this.gfdimg[index].chked = false
              for (var j = 0; j < this.currentimgList.length; j++) {
                if(this.currentimgList[j].img === this.gfdimg[index].img){
                  this.currentimgList.splice(j, 1)
                }
              }
              console.log(222, this.currentimgList)
              this.imgsum--
            }
          }
        } else {
          // 单选
          if (i === index) {
            this.gfdimg[index].chked = !this.gfdimg[index].chked
            this.currentimg = this.gfdimg[index].img
          } else {
            this.gfdimg[i].chked = false
          }
        }

      }
    },
    //完成
    GFwanchengwg() {
      if (this.panduan === '110') {
        //存储地址-多选
        uni.$emit('currentimgList', {
          currentimgList: this.currentimgList
        })
      } else {
        //存储地址-单选
        uni.$emit('currentImgs', {
          currentimg: this.currentimg
        })
      }
      uni.navigateBack()
    }
  }
}
